package com.zy.algorithm.v3.array

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.zy.activity.BaseFragment
import com.zy.algorithm.v3.R
import com.zy.view.DIYSortView
import com.zy.utils.data.Point
import com.zy.zlog.ZLog

abstract class BaseV3ArrayFragment : BaseFragment() {

    companion object {
        const val TAG = "BaseV3ArrayFragment"
    }

    override val layout: Int
        get() = R.layout.fragment_array_layout

    var code: Int = 0

    lateinit var diyV4: DIYSortView
    lateinit var title: TextView
    lateinit var opContainer: FrameLayout
    lateinit var opAddContainer: LinearLayout


    val builder = Point.Builder().setD(getInitD()).setPer(getInitPer())

    private val opReceiver = OPReceiver()

    //adb shell am broadcast -a com.zy.test.DataTest --ei code 1 --ei type 1
    inner class OPReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            ZLog.d(TAG, intent)
            onDIYReceive(intent)
        }
    }


    override fun onAttach(context: Context) {
        super.onAttach(context)
        val filter = IntentFilter("com.zy.test.DataTest")
        getContext()?.registerReceiver(opReceiver, filter)
    }

    override fun onDetach() {
        super.onDetach()
        context?.unregisterReceiver(opReceiver)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        code = arguments?.getInt("code", 0) ?: 0
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        diyV4 = view.findViewById(R.id.sort_view)
        title = view.findViewById(R.id.title)
        opContainer = view.findViewById(R.id.op_container)
        opAddContainer = view.findViewById(R.id.op_add_container)
        val clickBlack = view.findViewById<View>(R.id.click_black)
        clickBlack.setOnLongClickListener {
            autoMove(diyV4)
            false
        }

        val clickBlackRB = view.findViewById<View>(R.id.click_black_rb)
        clickBlackRB.setOnClickListener {
            autoClick()
        }

        diyV4.setD(getInitD())
        setData(diyV4)
    }

    protected fun addAction(name: String, listener: () -> Unit) {
        val button = Button(context)
        button.text = name
        button.setTextColor(Color.BLUE)
        button.setOnClickListener {
            opContainer.visibility = View.GONE
            mainRunPost(1000) {
                listener.invoke()
            }
        }
        opAddContainer.addView(button)
    }

    protected fun updateD(d: Int) {
        builder.setD(d).setPer((d / 2 - 5).toFloat())
        diyV4.setD(d)
        diyV4.invalidate()
    }

    private fun getInitPer(): Float {
        return (getInitD() / 2 - 5).toFloat()
    }

    protected open fun getInitD(): Int {
        return 200
    }

    protected open fun setData(diyV4: DIYSortView) {}
    protected open fun autoMove(diyV4: DIYSortView) {
        opContainer.visibility = View.VISIBLE
    }

    protected open fun autoClick() {
    }

    protected open fun onDIYReceive(intent: Intent) {}
}